package cn.zcj.studytest.controller;

import cn.zcj.studytest.ThrowData.Result;
import cn.zcj.studytest.domain.SignUp;
import cn.zcj.studytest.service.SignUpService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/admin")
@Slf4j
public class SignUpController {
    @Resource
    private SignUpService signUpService;

    // 注册
    @PostMapping("/signup")
    public Result register(@RequestBody SignUp signUp) {
        Result result = new Result();
        try {
            return signUpService.sinUpInsert(signUp);
        } catch (Exception e) {
            log.error("插入错误【{}】", e.getMessage(), e);
            result.setMsg("网络异常");
            result.setSuccess(false);
            return result;
        }
    }

    // 登录
    @PostMapping("/login")
    public Result signIn(@RequestBody SignUp signUp) {
        Result result = new Result();
        try {
            return signUpService.login(signUp);
        } catch (Exception e) {
            log.error("登录错误【{}】", e.getMessage(), e);
            result.setMsg("网络异常");
            result.setSuccess(false);
            return result;
        }
    }

    // 获取用户信息
    @PostMapping("/getUserInfo")
    public Result getUserInfo(@RequestBody SignUp signUp) {
        Result result = new Result();
        try {
            return signUpService.getUserInfo(signUp);
        } catch (Exception e) {
            log.error("获取用户信息错误【{}】", e.getMessage(), e);
            result.setMsg("网络异常");
            result.setSuccess(false);
            return result;
        }
    }

    // 充值
    @PostMapping("/recharge")
    public Result recharge(@RequestBody SignUp signUp) {
        Result result = new Result();
        try {
            return signUpService.recharge(signUp);
        } catch (Exception e) {
            log.error("充值错误【{}】", e.getMessage(), e);
            result.setMsg("网络异常");
            result.setSuccess(false);
            return result;
        }
    }
}
